<?php
/*
    OpenLib
    Copyright (C) 2011  Michael Gukov, Pham Thi Thuy Duong

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


namespace App\Views\Lib;

use Sun\Html;
use Sun\Html\Form;
use App\Models;
use App\Models\DB;

class Field extends Main {
    private $edit;
    public function __construct() {
        parent::__construct();
        $this->edit = new Models\Edition();
        $this->data_scene = 'FieldList';
    }


    public function AltScene() {
        if (empty($this->cond)) {
            $profile = $this->edit->GetFieldData($this->data['field_id']);
            $this->data = array_merge($this->data, $profile);
        }
        $this->IncludeScene("AltBody");
    }


    public function FieldList() {
        $fields = $this->edit->GetFieldsData();
        $select  = new Form\Select(array(
            'id' => "FieldListSelect",
            'class' => "WideListBox",
            'size' => "15" ));
        foreach ($fields as $field) {
            $select->AddOption($field['field_id'], $field['name']);
            if (@$this->data['field_id'] == $field['field_id'])
                $select->Added ()->selected = "selected";
        }
        if (empty($this->data['field_id']))
            $select->First()->selected = "selected";
        return $select;
    }

    public function DictSwitchPage() {
        $switch = new Html\PageSwitch("ViewDict");
        return $switch;
    }

    public function FieldTypes() {
        $field_type = $this->edit->db->Table("field_type")->fetchAll(DB::FETCH_ASSOC);
        $select  = new Form\Select(array(
            'id' => "type_id",
            'class' => "SelectBox"));
        foreach ($field_type as $type) {
            $select->AddOption($type['type_id'], $type['name']);
            if (@$this->data['type_id'] == $type['type_id'])
                $select->Added ()->selected = "selected";
        }
        if (empty($this->data['type_id']))
            $select->First()->selected = "selected";
        return $select;
    }

}
